package com.sqtracker.recommender.controller;

import com.sqtracker.recommender.dto.UserPreferenceDTO;
import com.sqtracker.recommender.service.UserPreferenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user-preferences")
@Tag(name = "用户偏好接口", description = "管理用户偏好设置的接口")
public class UserPreferenceController {

    private final UserPreferenceService userPreferenceService;

    @Autowired
    public UserPreferenceController(UserPreferenceService userPreferenceService) {
        this.userPreferenceService = userPreferenceService;
    }

    @GetMapping("/{userId}")
    @Operation(summary = "获取用户偏好", description = "获取指定用户的偏好设置")
    public ResponseEntity<?> getUserPreference(
            @Parameter(description = "用户ID") @PathVariable String userId) {

        return ResponseEntity.ok(userPreferenceService.getUserPreference(userId));
    }

    @PostMapping
    @Operation(summary = "设置用户偏好", description = "设置或更新用户的偏好")
    public ResponseEntity<?> setUserPreference(@RequestBody UserPreferenceDTO preferenceDTO) {
        userPreferenceService.saveUserPreference(preferenceDTO);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/options")
    @Operation(summary = "获取偏好选项", description = "获取可用的偏好设置选项")
    public ResponseEntity<?> getPreferenceOptions() {
        return ResponseEntity.ok(userPreferenceService.getPreferenceOptions());
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "重置用户偏好", description = "重置指定用户的所有偏好设置")
    public ResponseEntity<?> resetUserPreference(
            @Parameter(description = "用户ID") @PathVariable String userId) {

        userPreferenceService.resetUserPreference(userId);
        return ResponseEntity.ok().build();
    }
}